﻿
// First: Create config
ko.validation.configure({
	registerExtenders: true,
	messagesOnModified: true,
	insertMessages: true,
	parseInputAttributes: true,
	messageTemplate: null,
	decorateElement: true,
	errorClass: "error",
	errorMessageClass: "error-desc"
});
var mustEqual = function (val, other) {
	return val == other();
};

// Second: Create new model to apply to view
var viewModel = new function () {
    // About you
    this.EmailType = ko.observable().extend({ maxLength: 20 }).extend({ required: { message: "Email is required"} });
    this.SubjectLine = ko.observable().extend({ maxLength: 50 }).extend({ required: { message: "Subject Line is required"} });
    this.FromAddress = ko.observable().extend({ required: { message: 'Enter a valid email'} }).extend({ email: { message: 'Enter a valid email'} });
    this.CC = ko.observable();
    this.BodyText = ko.observable();
    this.Active = ko.observable(false);

    // Submit, Next button
    this.InvalidateForm = ko.observable(false);
    this.SubmitForm = function () {
        if (viewModel.errors().length == 0) {
            $('form').submit();
        }
        else {
            viewModel.errors.showAllMessages();
            this.InvalidateForm(true);
        }
    }
};

viewModel.errors = ko.validation.group(viewModel);

// Third: Apply view model to view to handle action when user input data
ko.validation.rules.pattern.message = 'Invalid.';
ko.applyBindings(viewModel, document.getElementById("main"));